/*
 * Copyright (C) 2017 Gautam Chibde
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.chibde.audiovisualizer.sample;

import com.chibde.audiovisualizer.util.PlaySoundUtil;
import com.hjq.permissions.OnPermission;
import com.hjq.permissions.XXPermissions;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.utils.Color;
import ohos.agp.window.dialog.ToastDialog;
import ohos.agp.window.service.WindowManager;

import java.io.IOException;
import java.util.List;

/**
 * BaseAbility that contains common code for all visualizers
 * <p>
 * Created by gautam chibde on 18/11/17.
 */
public abstract class BaseAbility extends Ability {
    protected PlaySoundUtil playSoundUtil;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        if (getLayout() != 0) {
            setUIContent(getLayout());
        }
        WindowManager.getInstance().getTopWindow().get().setStatusBarColor(Color.getIntColor("#303F9F"));

        // 权限
        getPermissions();
    }

    private void getPermissions() {
        XXPermissions.with(BaseAbility.this).permission("ohos.permission.MICROPHONE").request(new OnPermission() {
            @Override
            public void hasPermission(List<String> list, boolean b) {
                if (b) {
                    initialize();
                } else {
                    terminateAbility();
                }
            }

            @Override
            public void noPermission(List<String> list, boolean b) {
                if (b) {
                    // 如果是被永久拒绝就跳转到应用权限系统设置页面
                    String[] permissions = new String[list.size()];
                    list.toArray(permissions);
                    XXPermissions.startPermissionActivity(BaseAbility.this, permissions, this);
                } else {
                    new ToastDialog(getContext()).setText("获取权限失败").show();
                    terminateAbility();
                }
            }
        });
    }

    private void initialize() {
        setPlayer();
    }

    // 播放音乐的方法
    private void setPlayer() {
        try {
            playSoundUtil = new PlaySoundUtil();
        } catch (IOException e) {
            e.fillInStackTrace();
        }
        init();
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (playSoundUtil != null
                && playSoundUtil.getAudioRenderer() != null
                && playSoundUtil.getAudioRenderer().getState() != null) {
            playSoundUtil.getAudioRenderer().stop();
            playSoundUtil.getAudioRenderer().release();
        }
    }

    protected int getLayout() {
        return 0;
    }

    /**
     * init
     */
    public abstract void init();
}
